package zara

import java.util.Calendar
// ip纪录表，每一次成功
// 或不成功都纪录在案
class Ip {
    String source // 贡献的用户名
    String address // 贡献的用户的IP地址
    User person
    Date lastTime = new Date()
    Boolean success = false
    Website website
    User sitePerson
    static constraints = {
        source(size:1..50,blank:false )
        address(size:1..15,blank:false )
        lastTime()
        success()
        website()
        person()
        sitePerson()

    }

    String toString(){
        "${source} : ${address}"
    }

    static int lastIp(String ip){
        def calendar=Calendar.getInstance();   

        calendar.add(Calendar.HOUR_OF_DAY ,1)
        def end = calendar.getTime()
        calendar.add(Calendar.HOUR_OF_DAY ,-25)
        def start = calendar.getTime()
        def c = Ip.createCriteria()

        return c.count {
            and{
                between('lastTime', start, end)
                eq('success', true)
                eq('address', ip)
            } 
        }
        //return  Ip.countByAddressAndLastTimeBetween( ip , start , end)
    }
}
